<?php

class SiteController extends Controller
{
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: add common methods
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class' => 'CCaptchaAction',
				'backColor' => 0xFFFFFF,
			),
		);
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Add filete for this controller
	 */
	public function filters()
    {
        return compactor();
    }
	/**
	 * @Author: nguyen Tuan Kiet
	 * @Method: default action for this page (Home page)
	 */
	public function actionIndex()
	{
		$this->is_home = true;
		$this->layout = '//layouts/no_column';

		$this->pageTitle('Home Page');

		$this->render('index', array(
		));
	}
 	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Search bar action
	 */
	public function actionSearch()
	{
		$this->layout = '//layouts/no_column';
 
		$search_query = getQuery('search_query') ? getQuery('search_query') : '';
		$current_page = getQuery('page') ? getQuery('page') : 1;

		$sortings = array(
			'timestamp' => 't.timestamp',
			'name' => 'description.name',
			'price' => 't.price',
		);
		
		$sort_dirs = array(
			'asc' => 'ASC',
			'desc' => 'DESC',
		);
		
		$view_layouts = array(
			'grid' => array(),
			'list' => array(),
		);
		
		$view_type = getParam('view_layout');
		$sortby = getParam('sortby');
		$sortdir = getParam('sortdir');
		if ( empty($view_type) || !isset($view_layouts[$view_type]) ) $view_type = 'grid';
		if ( empty($sortby) || !isset($sortings[$sortby]) ) $sortby = 'timestamp';
		if ( empty($sortdir) || !isset($sort_dirs[$sortdir]) ) $sortdir = 'desc';
		
		$order = sprintf('%s %s', $sortings[$sortby], $sort_dirs[$sortdir]);
		
		$with = array(
			'description', 'seo', 'main_category',
			'main_image_pair.image' => array('alias' => 'main_image'),
			'feature.authors' => array('together' => true)
		);
		
		$criteria = array(
			'with' => $with,
			'order' => $order,
		);

		$this->breadcrumbs = array(
			sprintf(t('Search results for "%s"'), $search_query),
		);
		
		$this->pageTitle(sprintf(t('Search result for: %s'), $search_query));

		$criteria = new CDbCriteria($criteria);
		if ( $search_query ) {
			$criteria->addSearchCondition('description.name', $search_query, true, 'OR');
			$criteria->addSearchCondition('authors.name', $search_query, true, 'OR'); 
		}

		$criteria->addCondition('t.status="A"');
		$products = array();
		$total_products = array();
		
		$total_products = User::advanceSearchWithUser($search_query, $sortby, $sortdir, getItemsPerPage(), $current_page, user()->id, 'A');
	
		$pagination = new CPagination($total_products['total']);
		$pagination->pageSize = getItemsPerPage();
		//Phan loai total product
		foreach ($total_products['data'] as $key => $value) 
		{
			$id = $value{'id'};
			$is_user = $value{'is_user'};
			if($is_user)
			{
				//Setup user view here!!
				$products[] = User::model()->findByPk($id);
			}
		}
		$data = array(
			'products' => $products,
			'pagination' => $pagination,

			'sortby' => $sortby,
			'sortdir' => $sortdir,
			'view_type' => $view_type,
			'view_layout' => $view_layouts[$view_type],
		);
		
		if (isAjaxRequest() ) {
			$this->renderAjax('search', $data);
		} else {
			$this->render('search', $data);
		}
	}

	/**
	 * @Author: Nguyen TUan Kiet
	 * @Method: Error controller
	 */
	public function actionError()
	{
		$this->layout = '//layouts/no_column';
		
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	} 
}